using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameManager : MonoBehaviour {

    //Well, lists of lists aren't working in editor...hardcoding stages!
    //Add more as necessary, just add it into Start and it will work
    //Can be enemies, spawners, destructables...
    public List<GameObject> stage0Enemies;
    public List<GameObject> stage1Enemies;
    public List<GameObject> stage2Enemies;
    public List<GameObject> stage3Enemies;
	public List<GameObject> stage4Enemies;
	
    public GameObject rightRoadblock;
	public GameObject arrowToProceed;

    public int currentStage = 0;
    public bool currentStageComplete = false;

    public bool inFinalStage;
    public bool gameWin;

    private List<List<GameObject>> stages;

	// Use this for initialization
	void Start () {
        stages = new List<List<GameObject>>();
        stages.Add(stage0Enemies);
        stages.Add(stage1Enemies);
        stages.Add(stage2Enemies);
        stages.Add(stage3Enemies);
        stages.Add(stage4Enemies);

        currentStage = 0;
        currentStageComplete = false;
        rightRoadblock.collider.enabled = true;
		arrowToProceed.SetActive(false);

        DeactivateAllStages();
        ActivateCurrentStage();

        inFinalStage = false;
        gameWin = false;
	}
	
	// Update is called once per frame
	void Update () {
        if (!currentStageComplete)
        {
            List<GameObject> stage = stages[currentStage];

            currentStageComplete = true;
            foreach (GameObject enemy in stage)
            {
                if (enemy != null)
                    currentStageComplete = false;
            }

            if (currentStageComplete)
            {
                if (currentStage < stages.Count - 1)
                {
                    rightRoadblock.collider.enabled = false;
                    arrowToProceed.SetActive(true);
                }
                else
                {
                    gameWin = true;
                }
            }
        }
	}

    public int GetStageNum()
    {
        return currentStage;
    }
    public int GetNumStages()
    {
        return stages.Count;
    }

    public void AdvanceStage()
    {
        currentStageComplete = false;
        rightRoadblock.collider.enabled = true;
		arrowToProceed.SetActive(false);
        currentStage++;
        if (currentStage == stages.Count - 1)
            inFinalStage = true;
        ActivateCurrentStage();
    }

    void ActivateCurrentStage()
    {
        foreach (GameObject o in stages[currentStage])
        {
            o.SetActive(true);
        }
    }
    void DeactivateAllStages()
    {
        foreach (List<GameObject> l in stages)
        {
            foreach (GameObject o in l)
            {
                o.SetActive(false);
            }
        }
    }

    public void GoToWinScreen()
    {
        Application.LoadLevel("winscreen");
    }
}
